import pathToRegexp from 'path-to-regexp';
import {  addressAll, delAddressApi, saveAddressApi, edtAddressApi, addressItem } from './api';
import { Toast } from 'antd-mobile';
import { routerRedux } from 'dva/router';

export default {
    namespace: 'orderAddress',
    state: {
        addressEntities:[]

    },
    reducers: {
        save(state, { data }) {
            return { ...state, ...data };
        },

    },
    effects: {
        *settlementEFF({  }, { put, call }) {
            //  Toast.loading();
            const result = yield call(settlement);
            if (result.code == 200) {
                yield put({ type: 'save', data: { list: result.ret.list } });
                let ids =  result.ret.list.reduce((p,v,i,a)=>{
                    return p+v.cartIds
                },"")

                yield put({ type:"couponEFF", obj:{ cartIds:ids } });
                //Toast.success(result.msg, 1.5);
                return result.ret.list
            } else {
                Toast.fail(result.msg, 1.5);
            }
        },

        *couponEFF({ obj }, { put, call }){
            const result = yield call(coupon, obj);
            if (result.code == 200) {
                // yield put({ type: 'save', data: { list: result.ret.list } });
                //Toast.success(result.msg, 1.5);
            } else {
                Toast.fail(result.msg, 1.5);
            }
        },

        *addressAllEff({  }, { put, call }){
            const result = yield call(addressAll);
            if (result.code == 200) {
                // let defaultAddress = result.ret.addressEntities.filter((v,i,a)=>{
                //     return v.isDefault==1
                // });
                yield put({ type: 'save', data: { addressEntities: result.ret.addressEntities||[] } });
                //Toast.success(result.msg, 1.5);
            } else {
                Toast.fail(result.msg, 1.5);
            }
        },

        *delAddressEFF({ obj }, { select ,put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(delAddressApi,obj);
            if(result.code==200){
                Toast.hide();
                const orderAddress = yield select((state)=>state.orderAddress);
             // console.log(orderAddress);
                let addressEntities = orderAddress.addressEntities.filter((v,i,a)=>{
                    return v.id!=obj.id
                })
                yield put({ type: 'save', data: { addressEntities: addressEntities } });
                return true
            }else {
                Toast.fail(result.msg)
                return false
            }
            console.log('处理中')
           /// return result
        },

        *saveAddressEFF({ obj }, { select ,put, call }) {
            Toast.loading();
            const result = yield call(saveAddressApi,obj);
            if(result.code==200){
                Toast.hide();
                yield put(routerRedux.goBack());
            }else {
                Toast.fail(result.msg)
            }
        },

        *edtAddressApiEFF({ obj }, { select ,put, call }) {
            Toast.loading();
            const result = yield call(edtAddressApi,obj);
            if(result.code==200){
                Toast.hide();
                const orderAddress = yield select((state)=>state.orderAddress);
                let addressEntities = orderAddress.addressEntities.map((v,i,a)=>{
                    if(v.id==obj.id ){
                        return { ...v, isDefault:1 }
                    }else{
                        return { ...v, isDefault:0 }
                    }
                })
                yield put({ type: 'save', data: { addressEntities: addressEntities } });
            }else {
                Toast.fail(result.msg)
            }
        },

        *getAddressItemEFF({ obj }, { put, call }) {
            Toast.loading();
            const result = yield call(addressItem,obj);
            if(result.code==200){
                Toast.hide();
               return result.ret
            }else {
                Toast.fail(result.msg)
            }
            /// return result
        },


    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, query }) => {
                //console.log(match)
                if(pathname.includes('/orderAddress')){
                    // const match = pathToRegexp('/goodsDetail/:specId').exec(pathname);
                    //dispatch({ type:'settlementEFF' });
                    dispatch({ type:'addressAllEff' });
                }
            });
        },
    },
};
